package com.example.admin.brainwave.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.admin.brainwave.R;
import com.example.admin.brainwave.base.BaseActivity;
import com.example.admin.brainwave.bean.HistoryBean;
import com.example.admin.brainwave.net.HistoryTask;
import com.example.admin.brainwave.ui.adapter.HistoryAdapter;
import com.example.admin.brainwave.utils.Const;
import com.example.admin.brainwave.utils.ITaskCallbackListener;
import com.example.admin.brainwave.utils.RecycleViewDivider;
import com.example.admin.brainwave.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HistoryActivity extends BaseActivity {


    @BindView(R.id.iv_top_back)
    ImageView ivTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.rv_history_list)
    RecyclerView rvHistoryList;
    HistoryAdapter historyAdapter;
    private View noDataView;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_history;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText(getResources().getString(R.string.history_data));
        noDataView = getLayoutInflater().inflate(R.layout.layout_nodata, (ViewGroup) rvHistoryList.getParent(), false);
        rvHistoryList.setLayoutManager(new LinearLayoutManager(this));
        historyAdapter = new HistoryAdapter(R.layout.item_history, new ArrayList<>());
        rvHistoryList.setAdapter(historyAdapter);
        rvHistoryList.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.VERTICAL, 10, Color.parseColor("#f2f5f7")));
    }

    @Override
    protected void addListener() {
        ivTopBack.setOnClickListener(view -> finish());
        noDataView.setOnClickListener(v -> {
            startActivity(new Intent(HistoryActivity.this, BrainCollectionHomeActivity.class));
            finish();
        });
    }


    @Override
    protected void initData() {
        new HistoryTask(this, o -> {
            if (o != null) {
                HistoryBean bean = (HistoryBean) o;
                if (Const.OKCODE.equals(bean.getCode())) {
                    if (bean.getResult().size() > 0) {
                        historyAdapter.setNewData(bean.getResult());
                    } else {
                        historyAdapter.setEmptyView(noDataView);
                    }
                } else {
                    historyAdapter.setEmptyView(noDataView);
                    ToastUtil.showShort(HistoryActivity.this, bean.getMessage());
                }
            } else {
                historyAdapter.setEmptyView(noDataView);
            }
        }).execute();
    }
}
